/**
 * Copyright 2007-2010 非也
 * All rights reserved. 
 * 
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License v3 as published by the Free Software
 * Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, see http://www.gnu.org/licenses/lgpl.html.
 *
 */
package org.fireflow2.designer.eclipse.properties.common;

import javax.xml.namespace.QName;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.fireflow2.designer.eclipse.util.Util4Eclipse;

/**
 * 
 * @author 非也 nychen2000@163.com Fire Workflow 官方网站：www.firesoa.com 或者
 *         www.fireflow.org
 * 
 */
public class DataTypeSelectionDialog extends WizardDialog {

	/**
	 * @param parentShell
	 * @param newWizard
	 */
	public DataTypeSelectionDialog(Shell parentShell) {
		super(parentShell, new DataTypeWizard());
		((DataTypeWizard)this.getWizard()).setWizardDialog(this);
	}
	
	public QName getResult(){
		DataTypeWizard wizard = (DataTypeWizard)this.getWizard();
		return wizard.getResult();
	}
	
	public void okPressed2(){
		this.okPressed();
	}
}

class DataTypeWizard extends Wizard {
	QName result = null;
	WizardDialog dialog = null;
	public void setWizardDialog(WizardDialog dlg){
		dialog = dlg;
	}
	public DataTypeWizard() {

		this.setNeedsProgressMonitor(false);

		this.setWindowTitle("选择数据类型");

	}
	
	public void setResult(QName result){
		this.result = result;
	}

	public QName getResult(){
		return result;
	}
	
	public void okPressed(){
		((DataTypeSelectionDialog)dialog).okPressed2();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.wizard.Wizard#performFinish()
	 */
	@Override
	public boolean performFinish() {
		if (result!=null)return true;

		return false;
	}

	public void addPages() {

		DataTypeSelectionPage page = new DataTypeSelectionPage("Test");
		this.addPage(page);

	}
}

class DataTypeSelectionPage extends WizardPage {

	/**
	 * @param pageName
	 */
	protected DataTypeSelectionPage(String pageName) {
		super(pageName);
		this.setMessage("请选择数据类型");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
	 * .Composite)
	 */
	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout gridLayout = new GridLayout(1, false);
		composite.setLayout(gridLayout);

		final CTabFolder tabFolder = new CTabFolder(composite, SWT.NONE
				| SWT.BORDER);

		tabFolder.setLayoutData(new GridData(GridData.FILL_BOTH));

		// 下面是设置渐变色
		// Color[] color=new Color[4];
		// color[0]= ColorConstants.darkBlue;
		// color[1]= ColorConstants.blue;
		// color[2]= ColorConstants.darkGray;
		// color[3]= ColorConstants.white;
		// int[] intArray=new int[]{25,45,100};
		// tabFolder.setSelectionBackground(color, intArray);
		tabFolder.setSimple(false);// 设置圆角
		// tabFolder.setUnselectedCloseVisible(true);
		tabFolder.setSelectionBackground(Util4Eclipse
				.colorStr2ColorObj("#E6E6FA"));

		// 简单数据类型
		CTabItem item = new CTabItem(tabFolder, SWT.None | SWT.MULTI
				| SWT.V_SCROLL);
		item.setText("简单数据类型");
		Composite simpleDataPanel = new Composite(tabFolder, SWT.NONE);
		FillLayout fillLayout = new FillLayout();
		simpleDataPanel.setLayout(fillLayout);
		this.createSimpleDataTypePanel(simpleDataPanel);
		item.setControl(simpleDataPanel);

		item = new CTabItem(tabFolder, SWT.None | SWT.MULTI | SWT.V_SCROLL);
		item.setText("Java类型");
		Composite javaDataPanel = new Composite(tabFolder, SWT.NONE);
		javaDataPanel.setLayout(fillLayout);
		this.createJavaDataTypePanel(javaDataPanel);
		item.setControl(javaDataPanel);

		item = new CTabItem(tabFolder, SWT.None | SWT.MULTI | SWT.V_SCROLL);
		item.setText("Xml数据类型");
		Composite xmlDataPanel = new Composite(tabFolder, SWT.NONE);
		simpleDataPanel.setLayout(fillLayout);
		this.createXmlDataTypePanel(xmlDataPanel);
		item.setControl(xmlDataPanel);

		this.setControl(composite);
	}

	private void createSimpleDataTypePanel(Composite parent) {
		Composite contentPanel = new Composite(parent, SWT.NONE);
		contentPanel.setBackground(ColorConstants.button);
		contentPanel.setLayout(new FillLayout());
		ListViewer listViewer = new ListViewer(contentPanel, SWT.SINGLE
				| SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		listViewer.setContentProvider(new ArrayContentProvider());
		listViewer.setLabelProvider(new LabelProvider() {
			public String getText(Object element) {
				QName qname = (QName) element;
				return qname.getPrefix() + ":" + qname.getLocalPart();
			}
		});
		listViewer.setInput(Util4Eclipse.getBasicDataTypes());
		
		listViewer.addSelectionChangedListener(new ISelectionChangedListener(){

			public void selectionChanged(SelectionChangedEvent event) {
				ISelection selection = event.getSelection();
				if (selection instanceof StructuredSelection){
					QName qname = (QName)((StructuredSelection)selection).getFirstElement();
					((DataTypeWizard)getWizard()).setResult(qname);
				}
			}
			
		});
		
		listViewer.addDoubleClickListener(new IDoubleClickListener(){

			public void doubleClick(DoubleClickEvent event) {
				ISelection selection = event.getSelection();
				if (selection instanceof StructuredSelection){
					QName qname = (QName)((StructuredSelection)selection).getFirstElement();
					((DataTypeWizard)getWizard()).setResult(qname);
					
					((DataTypeWizard)getWizard()).okPressed();
				}
				
			}
			
		});
	}

	private void createJavaDataTypePanel(Composite parent) {
		JavaTypeSelectionPanel page = new JavaTypeSelectionPanel(this.getWizard(),parent, null,
				IJavaSearchConstants.CLASS);
		page.createControl(parent);

	}

	private void createXmlDataTypePanel(Composite parent) {
		Label lb = new Label(parent, SWT.NONE);
		lb.setText("test");
		// GridData gridData = new GridData();
		// lb.setLayoutData(gridData);
	}
}